import conf from '../conf'
import util from '../util'
import qs from 'qs';
import axios from 'axios'

const io = {

  configUrls: function () {
    //团购
    this.findCanActivityGroup = conf.baseApiPath + '/group-api/api/activityGroup/findCanActivityGroup'
    this.canAttActGro = conf.baseApiPath + '/group-api/api/activityGroup/canAttActGro'
    this.attendActivityGroup = conf.baseApiPath + '/group-api/api/activityGroup/attendActivityGroup'
    this.actGroDetByPhone = conf.baseApiPath + '/group-api/api/activityGroup/actGroDetByPhone'
    this.actGroDetById = conf.baseApiPath + '/group-api/api/activityGroup/actGroDetById'
    // 验证码
    this.generCaptcha = conf.baseApiPath + '/comm-api/api/captcha/generCaptcha'
    this.sendSms = conf.baseApiPath + '/comm-api/api/sms/sendSms'
  },
  getHeaders: function () {
    // const accessToken = util.getQueryString('accessToken')||'';
    const accessToken = localStorage.getItem('accessToken') || '';
    return {
      accessToken
    }

  },
  get: function (url, data, success, fail) {
    data.accessToken = this.getHeaders().accessToken
    $.ajax({
      url: url,
      type: 'GET',
      data: data,
      dataType: 'json',
      cache: true,
      timeout: 30000,
      success: function (data) {

        if (success) {
          success(data);
        }
      },
      error: function (xhr, status, error) {
        if (fail) {
          fail(xhr, status, error);
        } else {
          //alert("服务端出错");
        }
      }
    });
  },
  post: async function (url, data, success, fail) {
    try {
      data = {
         ...data,
        projectCode: conf.projectCode
      }
      let formdata = qs.stringify(data);
      let res = await axios({
        url: url,
        method: 'POST',
        data: formdata,
        dataType: 'json',
        timeout: 30000
      });
      console.log(res)
      if (res.data.desc === 'accessToken不存在或已过期') {
        document.location.href = conf.hostName
      }
      if (!res.data.success && res.data.desc) {
        window.alert(res.data.desc);
      }
      return res;
    } catch (err) {
      console.log(err)
      window.alert(err.name);
    }
  },
  syncPost: function (url, data, success, fail) {
    data.accessToken = this.getHeaders().accessToken
    $.ajax({
      url: url,
      type: 'POST',
      data: data,
      dataType: 'json',
      timeout: 30000,
      async: false,
      success: function (data) {
        // checkResult(data)
        if (success) {
          success(data);
        }
      },
      error: function (xhr, status, error) {
        if (fail) {
          fail(xhr, status, error);
        } else {
          //alert("服务端出错");
        }
      }
    });
  },
  postPlayload: function (url, data, success, fail) {
    data.accessToken = this.getHeaders().accessToken
    $.ajax({
      url: url,
      type: 'POST',
      contentType: 'application/json; charset=UTF-8',
      data: JSON.stringify(data),
      dataType: 'json',
      processData: false,
      timeout: 30000,
      success: function (data) {
        // checkResult(data)
        if (success) {
          success(data);
        }
      },
      error: function (xhr, status, error) {
        if (fail) {
          fail(xhr, status, error);
        } else {
          //alert("服务端出错");
        }
      }
    });
  },
  getCity: function () {
    var city = localStorage.getItem('city');
    var currentCity = sessionStorage.getItem('city');
    return city ? city : (currentCity ? currentCity : '广州')
  },
  ajax: function (method, url, data, cb) {
    if (ajaxTimes > 5) { return }
    data.accessToken = localStorage.getItem('accessToken');
    $.ajax({
      method: method,
      url: this.host + url,
      data: data,
      dataType: 'json',
      success: function (ret) {
        if (!ret.success && ret.desc && ret.desc.indexOf('oken') != -1) {

          getAccessToken(localStorage.getItem('openId'), function () {
            ajaxTimes++;
            io.ajax(method, url, data, cb)
            ajaxTimes = 0;
          })
        } else {
          ajaxTimes = 0;
          cb && cb(ret)
        }

      },
      error: function (error) {
        console.error(error);
      }
    });
  },
  ajaxWithoutToken: function (method, url, data, cb) {
    $.ajax({
      method: method,
      url: this.host + url,
      data: data,
      dataType: 'json',
      success: cb,
      error: function (error) {
        console.error(error);
      }
    });
  }
};

io.configUrls()

export default io;
